<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/include/taglib.jsp"%>
<html>
	<head>
		<meta charset="UTF-8">
		<title>我的账户-绑定快捷卡</title>
		<jsp:include page="/include/wchat/head.jsp"></jsp:include>

		<link type="text/css" rel="stylesheet"
			href="/v2/wchat/resrc/css/account.css">
	</head>
<style>
.link-touch .items {padding-right:20px;}
.itip ul.items{line-height:20px;}
</style>
	<body>
		<div id="header">
			<a href="javascript:history.go(-1);" class="backto">返回</a>
			<div class="title-sub">
				绑定快捷卡
			</div>
		</div>
		<div id="wrap">
			<form action="/allinPayment.do" method="POST"
				id="allinPaymentForm">
				<input type="hidden" name="SRCREQ_SN" id="SRCREQ_SN" value="" />
				<div id="bankCardForm">
					<div class="rechange">
						<div class="link-touch">
							<ul class="items">
								<li class="touch">
									<span class="txt">开户姓名</span>
									<span class="value">
										${realName }
									</span>
								</li>
								<li class="touch">
									<span class="txt">身份证号</span>
									<span class="value">
										${fn:substring(idNo, 0, 6)}********${fn:substring(idNo, 14, 18)}
									</span>
								</li>
							</ul>
						</div>
						<div class="link-touch">
							<ul class="items">
								<li class="touch">
									<span class="txt">银行</span>
									<span class="value">
									<select name="bankCheckSelect" id="bankCheckSelect"
										class="select" onchange="setBank();">
										<option value="0102">中国工商银行</option>
										<option value="0103">中国农业银行</option>
										<option value="0104">中国银行</option>
										<option value="0105">中国建设银行</option>
										<option value="0302">中信银行</option>
										<option value="0303">中国光大银行</option>
										<option value="0309">兴业银行</option>
										<option value="0403">中国邮政储蓄银行</option>

									</select>
									</span>
									<input type="hidden" name="bankName" id="bankName" value="" />
									<input type="hidden" name="bankCode" id="bankCode" value="" />
								</li>
								<li class="touch">
									<span class="txt">卡号</span>
									<span class="value">
									<input class="input" id="bankCardNumber" name="bankCardNumber"
										placeholder="银行卡号">
									</span>
									<input class="input" id="tranAmt" name="tranAmt"
										value="${tranAmt }" type="hidden">
								</li>
							</ul>

						</div>

						<div class="link-touch">
							<ul class="items">
								<li class="touch">
									<span class="txt">手机号</span>
									<span class="value">
										${cellPhone }
									</span>
								</li>
								<li class="touch">
									<span class="txt">校验码</span>
									<span class="value">
										<input class="input" id="verCode" name="verCode"
											   placeholder="请输入校验码">
										<a id="getVerCodeBtn" class="VerCodeBtn" href="javascript:getVerCodeBtn();">获取校验码</a>
									</span>
								</li>
							</ul>

							<div id="errorTip" class="vtip errs">
							</div>
						</div>
					</div>
					<div class="operate-button">
						<a href="javascript:void(0)" id="submit" class="btn">下一步</a>
					</div>

				</div>
			</form>
			<div id="itip" class="itip">
				<div class="title">
					<h2>
						说明：
					</h2>
				</div>
				<div>
					<ul class="items">
						<li>
							1. 每个用户<span class="light-org">只能绑定一张银行卡作为快捷卡</span>（以下称之为“快捷卡”），<span class="light-org">请选择常用银行卡进行绑定，且一旦绑定后，也只可以提现到快捷卡</span>；
						</li>
						<li>
							2.
							提现时，如已绑定快捷卡，不需重新绑定；如尚未绑定，<span class="light-org">请先绑定快捷卡作为提现卡</span>，同时其他绑定的提现银行卡将失效。<span class="light-org">提现申请时选择非快捷卡，提现将失败</span>；
						</li>
						<li>
							3. 为保障您账户资金的安全，<span class="light-org">如需换绑其他银行卡，需审核您的资料</span>，请联系共富网客服，提供相关资料；
						</li>
						<li>
							4. 客服确认信息及身份后，解除绑定，用户可从前台页面自行再次发起快捷绑卡及提现绑卡操作；
						</li>
						<li>
							5. 点击"下一步"按钮，<span class="light-org">表示您已经仔细阅读并同意此说明</span>。
						</li>
					</ul>
				</div>
			</div>

		</div>
		<jsp:include page="/include/wchat/footer.jsp"></jsp:include>

		<script type="text/javascript">
	$(function() {
		setBank();
		$("#submit").click(function() {
			addnewcard();
		});

	});

	function setBank() {
		var bankName = $("#bankCheckSelect").find("option:selected").text();
        var bankCode = $("#bankCheckSelect").val();
		$("#bankName").val(bankName);
		$("#bankCode").val(bankCode);
	}

	function addnewcard() {
	    var verCode = $.trim($("#verCode").val());
		var bankCard = $.trim($("#bankCardNumber").val());
        var SRCREQ_SN = $.trim($("#SRCREQ_SN").val());
        if(bankCard == "" || verCode == null){
            $("#errorTip").html("请输入银行卡号").show();
            return;
		}
        if (verCode == "" || verCode == null) {
            $("#errorTip").html("请输入校验码").show();
            return;
        }
        if (SRCREQ_SN == "" || SRCREQ_SN == null) {
            $("#errorTip").html("请求失败，请稍后再试").show();
            return;
        }
		if (cardNoCheck(bankCard)) {
            var param = {};
            param["paramMap.SRCREQ_SN"] = SRCREQ_SN;
            param["paramMap.verCode"] = verCode;
            $.ajax({
                type: 'POST',
                url: "/allinPaySmsCodeCheck.do",
                data: param,
                success: function(data) {
                    if(data.errCode == "success"){
                        window.location.href = data.checkUrl;
                    }else if(data.errCode == 1001){
                        $("#newPwdErrorTip").html("请求失败，请稍后再试！").show();
                        return;
                    }else if(data.errCode == 3998){
                        $("#errorTip").html("校验码错误，请重新输入！").show();
                        return;
                    }else{
                        $("#errorTip").html(data.msg).show();
                        return;
                    }
                }
            });
		}
	}

    function getVerCodeBtn() {
        var bankCard = $.trim($("#bankCardNumber").val());
        if(bankCard == "" || verCode == null){
            $("#errorTip").html("请输入银行卡号").show();
            return;
        }
        if($("#getAutoPwdBtn").hasClass("disableBtn")){
            return;
        }
        if (cardNoCheck(bankCard)) {
            var param = {}
            param["paramMap.bankName"] = $("#bankName").val(); //开户行
            param["paramMap.bankCode"] = $("#bankCode").val(); //开户行ID
            param["paramMap.bankCard"] = $.trim($("#bankCardNumber").val()); //卡号
            $.ajax( {
                dataType : 'json',
                url : environment.basePath + "/getAllinPayVerCode.do",
                data : param,
                type : 'POST',
                beforeSend : function() {
                    $('#errorTip').html("");
                },
                success : function(data) {
                    if (data.errCode == "success") {
                        $("#SRCREQ_SN").val(data.SRCREQ_SN);
                        alert("校验码已发送，请注意查收！")
                    } else{
                        $('#errorTip').html(data.msg);
                        return;
                    }
                    //倒计时
                    _interval = setInterval("autoPwdSend()", 1000);
                },
                error : function(data,status, e) {
                    $('#errorTip').html("*获取校验码失败，请稍后重试");
                }
            });
        }

    }

    /**
     * 60秒计时
     */
    var n = 60;
    /**
     * 连续执行方法句柄
     */
    var _interval;
    /**
     * 发送动态密码循环执行方法
     */
    function autoPwdSend(){
        n--;
        //获取校验码按钮 不可用
        $("#getVerCodeBtn").addClass("disableBtn");
        $("#getVerCodeBtn").attr("href","javascript:;").html(n + "秒后可重发");

        if (n == 0) {
            $("#getVerCodeBtn").removeClass("disableBtn");
            $("#getVerCodeBtn").attr("href","javascript:getVerCodeBtn();").html("获取校验码");
            clearInterval(_interval);
            n = 60;
        }
    }

	function cardNoCheck(bankno) {
		if (bankno == "" || bankno == null) {
			$('#errorTip').html("*卡号输入不正确，请检查");
			return false;
		}
		var lastNum = bankno.substr(bankno.length - 1, 1);

		var first15Num = bankno.substr(0, bankno.length - 1);
		var newArr = new Array();
		for ( var i = first15Num.length - 1; i > -1; i--) {
			newArr.push(first15Num.substr(i, 1));
		}
		var arrJiShu = new Array();
		var arrJiShu2 = new Array();

		var arrOuShu = new Array();
		for ( var j = 0; j < newArr.length; j++) {
			if ((j + 1) % 2 == 1) {
				if (parseInt(newArr[j]) * 2 < 9)
					arrJiShu.push(parseInt(newArr[j]) * 2);
				else
					arrJiShu2.push(parseInt(newArr[j]) * 2);
			} else
				arrOuShu.push(newArr[j]);
		}

		var jishu_child1 = new Array();
		var jishu_child2 = new Array();
		for ( var h = 0; h < arrJiShu2.length; h++) {
			jishu_child1.push(parseInt(arrJiShu2[h]) % 10);
			jishu_child2.push(parseInt(arrJiShu2[h]) / 10);
		}

		var sumJiShu = 0;
		var sumOuShu = 0;
		var sumJiShuChild1 = 0;
		var sumJiShuChild2 = 0;
		var sumTotal = 0;
		for ( var m = 0; m < arrJiShu.length; m++) {
			sumJiShu = sumJiShu + parseInt(arrJiShu[m]);
		}

		for ( var n = 0; n < arrOuShu.length; n++) {
			sumOuShu = sumOuShu + parseInt(arrOuShu[n]);
		}

		for ( var p = 0; p < jishu_child1.length; p++) {
			sumJiShuChild1 = sumJiShuChild1 + parseInt(jishu_child1[p]);
			sumJiShuChild2 = sumJiShuChild2 + parseInt(jishu_child2[p]);
		}
		sumTotal = parseInt(sumJiShu) + parseInt(sumOuShu)
				+ parseInt(sumJiShuChild1) + parseInt(sumJiShuChild2);

		var k = parseInt(sumTotal) % 10 == 0 ? 10 : parseInt(sumTotal) % 10;
		var luhm = 10 - k;

		if (parseInt(sumJiShu) + parseInt(sumOuShu) == 0) {
			$("#errorTip").html("*卡号输入不正确，请检查").show();
			return false;
		}

		if (lastNum == luhm) {
			return true;
		} else {
			$("#errorTip").html("*卡号输入不正确，请检查").show();
			return false;
		}
	}
</script>

	</body>
</html>